﻿using System;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
using System.Linq;
using System.Threading.Tasks;
//using AutoItX3Lib;
using System.Drawing;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Xml.Linq;
using System.IO;
using System.Drawing.Imaging;
using System.Diagnostics;
using CommonFunction;
using System.Diagnostics.CodeAnalysis;
using System.Security;

namespace RouletteD5
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public partial class Roulettist : Form
    {
        CancellationTokenSource tokenSource = null;
        Task taskPlayGame = null;

        public Roulettist()
        {
            InitializeComponent();
        }

        private void Roulettist_Load(object sender, EventArgs e)
        {
            //Common.HANDLER = this.BrowserGame.Handle;
            LoadMainData();
        }

        private void Roulettist_FormClosing(object sender, FormClosingEventArgs e)
        {
            var confirm = MessageBox.Show("Do you want to quit the Roulettist ?", "Roulettist Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (confirm != System.Windows.Forms.DialogResult.Yes) e.Cancel = true;
        }

        private void btnCofiguration_Click(object sender, EventArgs e)
        {
            FrmConfiguration frm = new FrmConfiguration();
            if (frm.InitForm()) frm.ShowDialog();
            frm = null;
            //IntPtr gameWindow = FindWindow(null, "5Dimes - Casino Lobby - Google Chrome");
            //if (gameWindow != IntPtr.Zero)
            //    SetWindowPos(gameWindow, IntPtr.Zero, 0, 0, 400, 400, SetWindowPosFlags.SWP_SHOWWINDOW);
        }

        private void btnPlayGame_Click(object sender, EventArgs e)
        {
            if (!ValidateBeforePlayGame()) return;

            ChangeTitleOfWindowGame();

            SwitchStatusOfPlayGame();

            Updated();

            NavigateGame();

            Common.GameStatus = Common.GAME_STATUS.LOADED;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {            
            this.Cursor = Cursors.WaitCursor;

            AccessData objData = new AccessData();
            var xmlDoc = objData.GetData();
            objData = null;
            if (!Common.InitGameInfo(xmlDoc))
            {
                MessageBox.Show("The Roulettist can not initialize the information of game", "Initializing Game Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = Cursors.Default;
                return;
            }

            Common.GameStatus = Common.GAME_STATUS.STARTED;
            
            FixedWindowGame();
            
            SwitchStatusOfStartOrStopBOT();

            //SelectBetRate();

            //tokenSource = new CancellationTokenSource();
            //Action playGame = delegate() { this.PlayGame(); };
            //taskPlayGame = new Task(playGame);
            //taskPlayGame.Start();

            this.Cursor = Cursors.Default;

            return;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            var confirm = MessageBox.Show("Do you want to STOP the Roulettist ?", "Roulettist Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (confirm != System.Windows.Forms.DialogResult.Yes) return;

            this.Cursor = Cursors.WaitCursor;

            Common.GameStatus = Common.GAME_STATUS.STOPPED;

            tokenSource.Cancel();
            tokenSource = null;
            taskPlayGame = null;

            SwitchStatusOfStartOrStopBOT();

            this.Cursor = Cursors.Default;
        }

        #region "processing the behaviour of controls"

        private void SwitchStatusOfPlayGame()
        {
            btnPlayGame.Enabled = false;
            btnStart.Enabled = true;

            txtRouletteUrl.Enabled = false;
            txtRoulettistName.Enabled = false;
        }

        private void ChangeTitleOfWindowGame()
        {
            this.Text = string.Format("{0} - {1}", this.Text, txtRoulettistName.Text.Trim());
            return;
        }

        private void NavigateGame()
        {
            //this.BrowserGame.Url = new Uri(txtRouletteUrl.Text.Trim());

            return;
        }

        private void SwitchStatusOfStartOrStopBOT()
        {
            var enabled = false;
            
            if (Common.GameStatus == Common.GAME_STATUS.STARTED)
                enabled = true;
            else
                enabled = false;

            setEnableButton(btnStart, !enabled);
            setEnableButton(btnStop, enabled);
            setEnableButton(btnCofiguration, !enabled);

            return;
        }

        #endregion

        #region "validating before playing game"

        private bool ValidateBeforePlayGame()
        {
            //validate the Url is empty or not
            if (string.IsNullOrEmpty(txtRouletteUrl.Text.Trim()))
            {
                MessageBox.Show("Please enter the Url of Roulette to play the game", "Roulettist Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                txtRouletteUrl.Focus();

                return false;
            }

            //validate the format the Url is invalid or not
            if (!Uri.IsWellFormedUriString(txtRouletteUrl.Text.Trim(), UriKind.Absolute))
            {
                MessageBox.Show("The Url of Roulette is invalid. Please enter the Url correctly", "Roulettist Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                txtRouletteUrl.Clear();
                txtRouletteUrl.Focus();

                return false;
            }

            //validate the name of roulettist is empty or not
            if (string.IsNullOrEmpty(txtRoulettistName.Text.Trim()))
            {
                MessageBox.Show("Please enter the name of Roulettist before playing the game", "Roulettist Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                txtRoulettistName.Focus();

                return false;
            }

            return true;
        }

        #endregion

        #region "load data of configuration"

        private void LoadMainData()
        {
            AccessData objData = null;
            XmlDocument xmlData = null;

            try
            {
                objData = new AccessData();
                xmlData = objData.GetData();

                txtRouletteUrl.Text = xmlData.GetRouletteURL();
                txtRoulettistName.Text = xmlData.GetRoulettistName();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Loading main data", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                xmlData = null;
                objData = null;
            }
        }

        private bool Updated()
        {
            AccessData objData = null;
            XmlDocument xmlData = null;
            bool success = false;

            try
            {
                objData = new AccessData();
                xmlData = objData.GetData();

                xmlData.SetRouletteURL(txtRouletteUrl.Text);
                xmlData.SetRoulettistName(txtRoulettistName.Text);

                objData.SaveData(xmlData);

                success = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Loading data configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                xmlData = null;
                objData = null;
            }

            return success;
        }

        #endregion

        #region "Play game"

        private void FixedWindowGame()
        {   
            //this.BrowserGame.Document.Window.ScrollTo(this.BrowserGame.Left + Common.SCROLL_HORIZONTAL, this.BrowserGame.Bottom);
            Thread.Sleep(100);
        }
        /*
        private void PlayGame()
        {
            bool breakGame = false;
            while (Common.GameStatus == Common.GAME_STATUS.STARTED)
            {
                if (Ready())
                {
                    var numberResult = Result();
                    if (numberResult == -1)
                    {
                        Thread.Sleep(2000);
                        continue;
                    }

                    Calculation(numberResult);
                    UpdateInfoOfGame();

                    if (Common.TOTAL_WINNINGS * 1000 >= Common.STOP_WINNING || Convert.ToUInt32(Math.Abs(Common.TOTAL_WINNINGS)) * 1000 >= Common.STOP_LOSING)
                    {
                        breakGame = true;
                        break;
                    }

                    if (Common.STRATEGY_TYPE == Common.STRATEGY.Following)
                        PlayFollowingStrategy(numberResult);
                    else
                        PlayOtherStrategy();

                    ConfirmBet();

                    Thread.Sleep(Common.SLEEP_SECOND);
                }
                else
                {
                    Thread.Sleep(2000);
                }
            }

            if (breakGame)
            {
                Common.GameStatus = Common.GAME_STATUS.STOPPED;
                tokenSource.Cancel();                
                tokenSource = null;
                taskPlayGame = null;
                SwitchStatusOfStartOrStopBOT();
                Common.RESUME_WAITING = DateTime.Now;
            }
        }
        */
        private void PlayFollowingStrategy(Int16 numberResult)
        {
            if (Common.GameStatus != Common.GAME_STATUS.STARTED) return;

            //black / red
            Common.LOST_FOLLOW_BLACK_RED = Common.LOST_FOLLOW_BLACK_RED > 29 ? (UInt16)29 : Common.LOST_FOLLOW_BLACK_RED;
            var noOfClick = Common.FOLLOWING_VALUES[(int)Common.STRATEGIES.BLACK_RED, Common.LOST_FOLLOW_BLACK_RED];
            if (noOfClick > 0)
            {
                //BetFollowingBlackRed(noOfClick, numberResult);
            }
            Thread.Sleep(500);

            //high / low
            Common.LOST_FOLLOW_HIGH_LOW = Common.LOST_FOLLOW_HIGH_LOW > 29 ? (UInt16)29 : Common.LOST_FOLLOW_HIGH_LOW;
            noOfClick = Common.FOLLOWING_VALUES[(int)Common.STRATEGIES.HIGH_LOW, Common.LOST_FOLLOW_HIGH_LOW];
            if (noOfClick > 0)
            {
                //BetFollowingHighLow(noOfClick, numberResult);
            }
            Thread.Sleep(500);

            //even / odd
            Common.LOST_FOLLOW_EVEN_ODD = Common.LOST_FOLLOW_EVEN_ODD > 29 ? (UInt16)29 : Common.LOST_FOLLOW_EVEN_ODD;
            noOfClick = Common.FOLLOWING_VALUES[(int)Common.STRATEGIES.EVEN_ODD, Common.LOST_FOLLOW_EVEN_ODD];
            if (noOfClick > 0)
            {
                //BetFollowingEvenOdd(noOfClick, numberResult);
            }
        }
        /*
        private void PlayOtherStrategy()
        {
            PlayBlackRed();
            Thread.Sleep(500);

            PlayEvenOdd();
            Thread.Sleep(500);

            PlayHighLow();
            Thread.Sleep(500);

            PlayDozens();
            Thread.Sleep(500);

            PlayColumns();
            Thread.Sleep(500);

            PlayNumbers();
        }
        
        private void BetFollowingBlackRed(UInt16 noOfClick, Int16 numberResult)
        {
            AutoItX3 autoit = new AutoItX3();
            if (IsRed(numberResult))
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.RED_X, Common.RED_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 0] = (UInt32)Common.BET_RATE * noOfClick;
            }
            else
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.BLACK_X, Common.BLACK_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 1] = (UInt32)Common.BET_RATE * noOfClick;
            }
            autoit = null;
        }

        private void BetFollowingEvenOdd(UInt16 noOfClick, Int16 numberResult)
        {
            AutoItX3 autoit = new AutoItX3();
            if (IsEven(numberResult))
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.EVEN_X, Common.EVEN_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 0] = (UInt32)Common.BET_RATE * noOfClick;
            }
            else
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.ODD_X, Common.ODD_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 1] = (UInt32)Common.BET_RATE * noOfClick;
            }
            autoit = null;
        }

        private void BetFollowingHighLow(UInt16 noOfClick, Int16 numberResult)
        {
            AutoItX3 autoit = new AutoItX3();
            if (IsHigh(numberResult))
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.HIGH_X, Common.HIGH_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 0] = (UInt32)Common.BET_RATE * noOfClick;
            }
            else
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.LOW_X, Common.LOW_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 1] = (UInt32)Common.BET_RATE * noOfClick;
            }
            autoit = null;
        }

        private void PlayBlackRed()
        {
            if (Common.GameStatus != Common.GAME_STATUS.STARTED || Common.WAITING_BR <= 0) return;

            AutoItX3 autoit = new AutoItX3();

            var index = Common.WAIT_BLACK - Common.WAITING_BR;
            UInt16 noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.BLACK_RED, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.BLACK_X, Common.BLACK_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 1] = (UInt32)Common.BET_RATE * noOfClick;
            }

            index = Common.WAIT_RED - Common.WAITING_BR;
            noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.BLACK_RED, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.RED_X, Common.RED_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 0] = (UInt32)Common.BET_RATE * noOfClick;
            }

            autoit = null;
        }

        private void PlayHighLow()
        {
            if (Common.GameStatus != Common.GAME_STATUS.STARTED || Common.WAITING_HL <= 0) return;

            AutoItX3 autoit = new AutoItX3();

            var index = Common.WAIT_HIGH - Common.WAITING_HL;
            UInt16 noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.HIGH_LOW, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.HIGH_X, Common.HIGH_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 0] = (UInt32)Common.BET_RATE * noOfClick;
            }

            index = Common.WAIT_LOW - Common.WAITING_HL;
            noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.HIGH_LOW, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.LOW_X, Common.LOW_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 1] = (UInt32)Common.BET_RATE * noOfClick;
            }

            autoit = null;
        }

        private void PlayEvenOdd()
        {
            if (Common.GameStatus != Common.GAME_STATUS.STARTED || Common.WAITING_EO <= 0) return;

            AutoItX3 autoit = new AutoItX3();

            var index = Common.WAIT_EVEN - Common.WAITING_EO;
            UInt16 noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.EVEN_ODD, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.EVEN_X, Common.EVEN_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 0] = (UInt32)Common.BET_RATE * noOfClick;
            }

            index = Common.WAIT_ODD - Common.WAITING_EO;
            noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.EVEN_ODD, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.ODD_X, Common.ODD_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 1] = (UInt32)Common.BET_RATE * noOfClick;
            }

            autoit = null;
        }

        private void PlayDozens()
        {
            if (Common.GameStatus != Common.GAME_STATUS.STARTED || Common.WAITING_DZ <= 0) return;

            AutoItX3 autoit = new AutoItX3();

            var index = Common.WAIT_DOZEN1 - Common.WAITING_DZ;
            UInt16 noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.DOZENS, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.DOZEN1_X, Common.DOZEN_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 0] = (UInt32)Common.BET_RATE * noOfClick;
            }

            index = Common.WAIT_DOZEN2 - Common.WAITING_DZ;
            noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.DOZENS, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.DOZEN2_X, Common.DOZEN_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 1] = (UInt32)Common.BET_RATE * noOfClick;
            }

            index = Common.WAIT_DOZEN3 - Common.WAITING_DZ;
            noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.DOZENS, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.DOZEN3_X, Common.DOZEN_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 2] = (UInt32)Common.BET_RATE * noOfClick;
            }

            autoit = null;
        }

        private void PlayColumns()
        {
            if (Common.GameStatus != Common.GAME_STATUS.STARTED || Common.WAITING_CL <= 0) return;

            AutoItX3 autoit = new AutoItX3();

            var index = Common.WAIT_COLUMN1 - Common.WAITING_CL;
            UInt16 noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.COLUMNS, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.COL1_X, Common.COL1_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 0] = (UInt32)Common.BET_RATE * noOfClick;
            }

            index = Common.WAIT_COLUMN2 - Common.WAITING_CL;
            noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.COLUMNS, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.COL2_X, Common.COL2_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 1] = (UInt32)Common.BET_RATE * noOfClick;
            }

            index = Common.WAIT_COLUMN3 - Common.WAITING_CL;
            noOfClick = 0;
            index = index > 29 ? 29 : index;
            if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.COLUMNS, index];
            if (noOfClick >= 0)
            {
                autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.COL3_X, Common.COL3_Y);
                Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 2] = (UInt32)Common.BET_RATE * noOfClick;
            }

            autoit = null;
        }

        private void PlayNumbers()
        {
            if (Common.GameStatus != Common.GAME_STATUS.STARTED || Common.WAITING_NO <= 0) return;

            AutoItX3 autoit = new AutoItX3();
            var cols1 = new UInt16[] { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36 };
            var cols2 = new UInt16[] { 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35 };
            
            for (int i = 0; i < 37; i++)
            {
                var index = Common.WAIT_NUMBER[i] - Common.WAITING_NO;
                UInt16 noOfClick = 0;
                index = index > 29 ? 29 : index;
                if (index >= 0) noOfClick = Common.OTHER_VALUES[(int)Common.STRATEGIES.NUMBERS, index];
                if (noOfClick >= 0)
                {
                    if (i == 0)
                        autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.NO_0_X, Common.NO_0_Y);
                    else if (cols1.Contains((UInt16)i))
                        autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.NO_COL1_X, Common.NO_COL1_Y);
                    else if (cols2.Contains((UInt16)i))
                        autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.NO_COL2_X, Common.NO_COL2_Y);
                    else
                        autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", noOfClick, Common.NO_COL3_X, Common.NO_COL3_Y);
                    Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.NUMBERS, i] = (UInt32)Common.BET_RATE * noOfClick;
                }
                Thread.Sleep(200);
            }
            
            autoit = null;
        }
        */
        private void Calculation(Int16 numberResult)
        {
            if (Common.LAST_NUMBER_RESULT == -1) Common.LAST_NUMBER_RESULT = numberResult;

            bool oldRed = IsRed(Common.LAST_NUMBER_RESULT);
            bool newRed = IsRed(numberResult);
            bool oldEven = IsEven(Common.LAST_NUMBER_RESULT);
            bool newEven = IsEven(numberResult);
            bool oldHigh = IsHigh(Common.LAST_NUMBER_RESULT);
            bool newHigh = IsHigh(numberResult);

            //calc for following strategy
            if (numberResult == 0)
            {
                Common.LOST_FOLLOW_BLACK_RED++;
                Common.LOST_FOLLOW_HIGH_LOW++;
                Common.LOST_FOLLOW_EVEN_ODD++;
                Common.WAIT_BLACK++;
                Common.WAIT_RED++;
                Common.WAIT_LOW++;
                Common.WAIT_HIGH++;
                Common.WAIT_EVEN++;
                Common.WAIT_ODD++;
                Common.WAIT_COLUMN1++;
                Common.WAIT_COLUMN2++;
                Common.WAIT_COLUMN3++;
                Common.WAIT_DOZEN1++;
                Common.WAIT_DOZEN2++;
                Common.WAIT_DOZEN3++;
                Common.WAIT_NUMBER[0] = 0;

                for (int i = 1; i < 37; i++)
                {
                    Common.WAIT_NUMBER[i]++;
                    Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.NUMBERS, i];
                }
                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.NUMBERS, 0];

                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 0];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 1];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 0];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 1];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 0];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 1];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 0];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 1];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 2];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 0];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 1];                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 2];                
            }
            else
            {
                if (oldRed == newRed)
                    Common.LOST_FOLLOW_BLACK_RED = 0;
                else
                    Common.LOST_FOLLOW_BLACK_RED++;

                if (oldEven == newEven)
                    Common.LOST_FOLLOW_EVEN_ODD = 0;
                else
                    Common.LOST_FOLLOW_EVEN_ODD++;

                if (oldHigh == newHigh)
                    Common.LOST_FOLLOW_HIGH_LOW = 0;
                else
                    Common.LOST_FOLLOW_HIGH_LOW++;
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.NUMBERS, 0];
            }

            //calc for black/red strategy
            if (newRed == false)
            {
                Common.WAIT_RED++;
                Common.WAIT_BLACK = 0;
                
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 0];
                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 1];
            }
            else
            {
                Common.WAIT_BLACK++;
                Common.WAIT_RED = 0;

                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 0];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 1];
            }

            //calc for high/low strategy
            if (IsHigh(numberResult))
            {
                Common.WAIT_LOW++;
                Common.WAIT_HIGH = 0;

                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 0];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 1];
            }
            else
            {
                Common.WAIT_HIGH++;
                Common.WAIT_LOW = 0;

                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 0];
                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 1];
            }

            //calc for EVEN/ODD strategy
            if (IsEven(numberResult))
            {
                Common.WAIT_ODD++;
                Common.WAIT_EVEN = 0;

                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 0];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 1];
            }
            else
            {
                Common.WAIT_EVEN++;
                Common.WAIT_ODD = 0;

                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 0];
                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 1];
            }

            //calc for DOZENS strategy
            var dozenResult = DozenIndex(numberResult);
            if (dozenResult== Common.DOZENS.D1)
            {
                Common.WAIT_DOZEN2++;
                Common.WAIT_DOZEN3++;
                Common.WAIT_DOZEN1 = 0;

                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 0];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 1];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 2];
            }
            else if (dozenResult == Common.DOZENS.D2)
            {
                Common.WAIT_DOZEN1++;
                Common.WAIT_DOZEN3++;
                Common.WAIT_DOZEN2 = 0;

                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 0];
                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 1];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 2];
            }
            else
            {
                Common.WAIT_DOZEN1++;
                Common.WAIT_DOZEN2++;
                Common.WAIT_DOZEN3 = 0;

                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 0];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 1];
                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 2];
            }

            //calc for COLUMNS strategy
            var columnResult = ColumnIndex(numberResult);
            if (columnResult == Common.COLUMNS.C1)
            {
                Common.WAIT_COLUMN2++;
                Common.WAIT_COLUMN3++;
                Common.WAIT_COLUMN1 = 0;

                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 0];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 1];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 2];
            }
            else if (columnResult == Common.COLUMNS.C2)
            {
                Common.WAIT_COLUMN1++;
                Common.WAIT_COLUMN3++;
                Common.WAIT_COLUMN2 = 0;

                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 0];
                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 1];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 2];
            }
            else
            {
                Common.WAIT_COLUMN1++;
                Common.WAIT_COLUMN2++;
                Common.WAIT_COLUMN3 = 0;

                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 0];
                Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 1];
                Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 2];
            }

            //calc for NUMBER strategy
            if (numberResult > 0)
            {
                for (int i = 0; i < 37; i++)
                {
                    if (numberResult == i)
                    {
                        Common.WAIT_NUMBER[i] = 0;
                        Common.TOTAL_WINNINGS += Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.NUMBERS, i];
                    }
                    else
                    {
                        Common.WAIT_NUMBER[i]++;
                        Common.TOTAL_WINNINGS -= Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.NUMBERS, i];
                    }
                }
            }

            Common.LAST_NUMBER_RESULT = numberResult;

            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 0] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.BLACK_RED, 1] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 0] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.HIGH_LOW, 1] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 0] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.EVEN_ODD, 1] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 0] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 1] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.COLUMNS, 2] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 0] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 1] = 0;
            Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.DOZENS, 2] = 0;
            for (int i = 0; i < 37; i++) Common.TOTAL_BET_AMOUNT[(int)Common.STRATEGIES.NUMBERS, i] = 0;

            return;
        }

        private void UpdateInfoOfGame()
        {
            setTextControl(lblBlack.Name, Common.WAIT_BLACK.ToString());
            setTextControl(lblRed.Name, Common.WAIT_RED.ToString());
            setTextControl(lblEven.Name, Common.WAIT_EVEN.ToString());
            setTextControl(lblOdd.Name, Common.WAIT_ODD.ToString());
            setTextControl(lblHigh.Name, Common.WAIT_HIGH.ToString());
            setTextControl(lblLow.Name, Common.WAIT_LOW.ToString());
            setTextControl(lblD1.Name, Common.WAIT_DOZEN1.ToString());
            setTextControl(lblD2.Name, Common.WAIT_DOZEN2.ToString());
            setTextControl(lblD3.Name, Common.WAIT_DOZEN3.ToString());
            setTextControl(lblC1.Name, Common.WAIT_COLUMN1.ToString());
            setTextControl(lblC2.Name, Common.WAIT_COLUMN2.ToString());
            setTextControl(lblC3.Name, Common.WAIT_COLUMN3.ToString());
            if (Common.LAST_NUMBER_RESULT >= 0) setTextControl(lblLastNumber.Name, Common.LAST_NUMBER_RESULT.ToString());

            var lblName = string.Empty;
            for (int i = 0; i < 37; i++)
            {
                lblName = string.Format("lbl{0}", i);
                setTextControl(lblName, Common.WAIT_NUMBER[i].ToString());
            }
            string totalWinning = (Common.TOTAL_WINNINGS * 1000).ToString("#,##0");
            setTextControl(lblTotalWinnings.Name, totalWinning);
            
            return;
        }

        delegate void SetTextCallback(string ctrlName, string text);

        private void setTextControl(string ctrlName, string text)
        {
            if (this.grpGameInfo.Controls[ctrlName].InvokeRequired)
            {
                SetTextCallback setText = new SetTextCallback(setTextControl);
                this.Invoke(setText, ctrlName, text);
            }
            else
                grpGameInfo.Controls[ctrlName].Text = text;
        }

        delegate void SetEnableButtonCallback(Button btn, bool isEnabled);

        private void setEnableButton(Button btn, bool isEnabled)
        {
            if (btn.InvokeRequired)
            {
                SetEnableButtonCallback setEnabled = new SetEnableButtonCallback(setEnableButton);
                this.Invoke(setEnabled, btn, isEnabled);
            }
            else
                btn.Enabled = isEnabled;
        }

        #endregion

        #region "Integrating with browser game"

        private bool Ready()
        {
            try
            {
                if (Common.PicReady == null)
                {
                    Common.PicReady = (Bitmap)Bitmap.FromFile(Common.ReadyFilePath);
                }

                var imgReady = CaptureImage(Common.HANDLER, Common.IMAGE_READY_X, Common.IMAGE_READY_Y, Common.IMAGE_WIDTH, Common.IMAGE_HEIGHT);
                
                imgReady.Save(Common.ResultFilePath);
                imgReady = Image.FromFile(Common.ResultFilePath);
                
                var compare = Common.PicReady.EqualTo(imgReady);
                
                imgReady.Dispose();

                return compare;
            }
            catch
            {
                return false;
            }
        }

        private Int16 Result()
        {
            Int16 returnValue = -1;
            try
            {
                var imgResult = CaptureImage(Common.HANDLER, Common.IMAGE_RESULT_X, Common.IMAGE_RESULT_Y, Common.IMAGE_RESULT_WIDTH, Common.IMAGE_RESULT_HEIGHT);
                imgResult.Save(Common.ResultFilePath);
                imgResult = Image.FromFile(Common.ResultFilePath);

                var numberPath = string.Empty;
                var numberName = string.Empty;
                Image imgNumber = null;

                for (Int16 i = 0; i < 37; i++)
                {
                    numberName = string.Format("{0}.jpg", i);
                    numberPath = System.IO.Path.Combine(Application.StartupPath, "Data", numberName);
                    if (!System.IO.File.Exists(numberPath)) continue;

                    imgNumber = Image.FromFile(numberPath);
                    var compare = imgNumber.EqualTo(imgResult);
                    imgNumber.Dispose();

                    if (compare)
                    {
                        returnValue = i;
                        break;
                    }
                }
                imgResult.Dispose();
            }
            catch { }

            return returnValue;
        }

        private bool IsRed(Int16 numberResult)
        {
            var redNumbers = new Int16[] { 1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 };

            return redNumbers.Contains(numberResult);
        }

        private bool IsHigh(Int16 numberResult)
        {
            return numberResult >= 19;
        }

        private bool IsEven(Int16 numberResult)
        {
            return (numberResult % 2m) == 0;
        }

        private Common.COLUMNS ColumnIndex(Int16 numberResult)
        {
            Common.COLUMNS columnResult = Common.COLUMNS.C1;

            var cols1 = new Int16[] { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36 };
            var cols2 = new Int16[] { 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35 };
            var cols3 = new Int16[] { 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34 };

            if (cols1.Contains(numberResult))
                columnResult = Common.COLUMNS.C1;
            else if (cols2.Contains(numberResult))
                columnResult = Common.COLUMNS.C2;
            else if (cols3.Contains(numberResult))
                columnResult = Common.COLUMNS.C3;

            return columnResult;
        }

        private Common.DOZENS DozenIndex(Int16 numberResult)
        {
            Common.DOZENS dozenResult = Common.DOZENS.D1;

            if (numberResult <= 12)
                dozenResult = Common.DOZENS.D1;
            else if (numberResult <= 24)
                dozenResult = Common.DOZENS.D2;
            else
                dozenResult = Common.DOZENS.D3;

            return dozenResult;
        }
        /*
        private void SelectBetRate()
        {
            AutoItX3 autoit = new AutoItX3();
            switch (Common.BET_RATE)
            {
                case 10:
                    autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", 1, Common.BET_RATE1_X, Common.BET_RATE_Y);
                    break;

                case 20:
                    autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", 1, Common.BET_RATE2_X, Common.BET_RATE_Y);
                    break;

                case 50:
                    autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", 1, Common.BET_RATE3_X, Common.BET_RATE_Y);
                    break;

                case 100:
                    autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", 1, Common.BET_RATE4_X, Common.BET_RATE_Y);
                    break;

                case 200:
                    autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", 1, Common.BET_RATE5_X, Common.BET_RATE_Y);
                    break;

                case 500:
                    autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", 1, Common.BET_RATE6_X, Common.BET_RATE_Y);
                    break;

                default: break;
            }
            autoit = null;
        }

        private void ConfirmBet()
        {
            AutoItX3 autoit = new AutoItX3();
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "LEFT", 1, Common.CONFIRM_BET_X, Common.CONFIRM_BET_Y);
            autoit = null;
        }
        */
        #endregion

        #region "proccess images"
        
        private void AutoUpdate()
        {
            try
            {
                //capture ready image
                if (Common.PicReady != null)
                {
                    Common.PicReady.Dispose();
                    Common.PicReady = null;                    
                }
                
                var image = CaptureImage(Common.HANDLER, Common.IMAGE_READY_X, Common.IMAGE_READY_Y, Common.IMAGE_WIDTH, Common.IMAGE_HEIGHT);
                image.Save(Common.ReadyFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                image.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Auto update failed ! \n{0}", ex.Message), "Auto update information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);        
        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
        [DllImport("user32.dll")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
        public const int SRCCOPY = 13369376;

        private Image CaptureImage(IntPtr handle, int posX, int posY, int width, int height)
        {

            IntPtr hdcSrc = GetWindowDC(handle);
            
            IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);

            IntPtr hOld = SelectObject(hdcDest, hBitmap);
            BitBlt(hdcDest, 0, 0, width, height, hdcSrc, posX, posY, SRCCOPY);
            SelectObject(hdcDest, hOld);
            DeleteDC(hdcDest);
            ReleaseDC(handle, hdcSrc);

            Image image = Image.FromHbitmap(hBitmap);
            DeleteObject(hBitmap);

            return image;
        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            //IntPtr hWnd = Process.GetProcesses().Where(p => p.MainWindowTitle.Contains("Roulettist on Facebook - Mozilla Firefox")).Select(p => p.MainWindowHandle).FirstOrDefault();
            
            ////FixedWindowGame();
            
            ////var image = CaptureImage(Common.HANDLER, Common.IMAGE_AMOUNT_X, Common.IMAGE_AMOUNT_Y, Common.IMAGE_AMOUNT_WIDTH, Common.IMAGE_AMOUNT_HEIGHT);
            //AutoItX3 autoit = new AutoItX3();
            //var postX = autoit.ControlGetPosX("Roulettist on Facebook - Mozilla Firefox", "", "[CLASS:Unity.WebPlayer; INSTANCE:1]");
            //var postY = autoit.ControlGetPosY("Roulettist on Facebook - Mozilla Firefox", "", "[CLASS:Unity.WebPlayer; INSTANCE:1]");
            //var frameWidth = autoit.ControlGetPosWidth("Roulettist on Facebook - Mozilla Firefox", "", "[CLASS:Unity.WebPlayer; INSTANCE:1]");
            //var framehheight = autoit.ControlGetPosHeight("Roulettist on Facebook - Mozilla Firefox", "", "[CLASS:Unity.WebPlayer; INSTANCE:1]");

            //postX = postX + Convert.ToInt32(textBox1.Text);
            //postY = postY + Convert.ToInt32(textBox2.Text);
            //frameWidth = Convert.ToInt32(textBox3.Text);
            //framehheight = Convert.ToInt32(textBox4.Text);

            //var image = CaptureImage(hWnd, postX, postY, frameWidth, framehheight);

            //image.Save(Common.ResultFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);

            //image.Dispose();
            //autoit = null;
        }

        private void picContact_Click(object sender, EventArgs e)
        {
            //frmRegistration frm = new frmRegistration();
            //frm.ShowDialog();
            //frm.Dispose();
        }

        #region "import DLL methods"

        [Flags]
        public enum SetWindowPosFlags : uint
        {
            // ReSharper disable InconsistentNaming

            /// <summary>
            ///     If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
            /// </summary>
            SWP_ASYNCWINDOWPOS = 0x4000,

            /// <summary>
            ///     Prevents generation of the WM_SYNCPAINT message.
            /// </summary>
            SWP_DEFERERASE = 0x2000,

            /// <summary>
            ///     Draws a frame (defined in the window's class description) around the window.
            /// </summary>
            SWP_DRAWFRAME = 0x0020,

            /// <summary>
            ///     Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
            /// </summary>
            SWP_FRAMECHANGED = 0x0020,

            /// <summary>
            ///     Hides the window.
            /// </summary>
            SWP_HIDEWINDOW = 0x0080,

            /// <summary>
            ///     Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
            /// </summary>
            SWP_NOACTIVATE = 0x0010,

            /// <summary>
            ///     Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
            /// </summary>
            SWP_NOCOPYBITS = 0x0100,

            /// <summary>
            ///     Retains the current position (ignores X and Y parameters).
            /// </summary>
            SWP_NOMOVE = 0x0002,

            /// <summary>
            ///     Does not change the owner window's position in the Z order.
            /// </summary>
            SWP_NOOWNERZORDER = 0x0200,

            /// <summary>
            ///     Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.
            /// </summary>
            SWP_NOREDRAW = 0x0008,

            /// <summary>
            ///     Same as the SWP_NOOWNERZORDER flag.
            /// </summary>
            SWP_NOREPOSITION = 0x0200,

            /// <summary>
            ///     Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
            /// </summary>
            SWP_NOSENDCHANGING = 0x0400,

            /// <summary>
            ///     Retains the current size (ignores the cx and cy parameters).
            /// </summary>
            SWP_NOSIZE = 0x0001,

            /// <summary>
            ///     Retains the current Z order (ignores the hWndInsertAfter parameter).
            /// </summary>
            SWP_NOZORDER = 0x0004,

            /// <summary>
            ///     Displays the window.
            /// </summary>
            SWP_SHOWWINDOW = 0x0040,

            // ReSharper restore InconsistentNaming
        }

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags uFlags);

        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
        [DllImport("User32", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr FindWindow
        (
            [MarshalAs(UnmanagedType.LPTStr)] string lpClassName,  // class name
            [MarshalAs(UnmanagedType.LPTStr)] string lpWindowName  // window name
        );

        #endregion
    }   
}